package com.chat.server;

import com.chat.data.MySqlSingle;
import com.chat.entity.DataPackage;
import com.chat.entity.File;
import com.chat.entity.User;
import com.chat.mapper.FileMapper;
import com.chat.mapper.UserMapper;
import com.chat.util.JwtUtils;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户服务
 */
public class UserServer implements DataPackagerServer{

    UserMapper userMapper;
    FileMapper fileMapper;
    SqlSession session;

    public UserServer() throws IOException {
        session = MySqlSingle.getMySqlSingle().getSqlSessionFactory().openSession();
        userMapper = MySqlSingle.getMySqlSingle().getSqlSessionFactory().getConfiguration().getMapper(UserMapper.class,session);
        fileMapper = MySqlSingle.getMySqlSingle().getSqlSessionFactory().getConfiguration().getMapper(FileMapper.class,session);
    }

    User selectUserById(int id){
        User user = userMapper.selectUserById(id);
        return user;
    }

    List<User> selectUsers(){
        return userMapper.selectUsers();
    }

    List<User> selectUserByRoom(int roomId){
        return userMapper.selectUsersByRoom(roomId);
    }

    @Override
    public DataPackage run(DataPackage source) {
        Map<String,Object> param = (Map<String, Object>) source.getDate();
        switch (source.getAction()){
            case GET_USER://获取请求者的个人信息
                String token = source.getToken();
                String account = JwtUtils.getAccount(token);
                User user = userMapper.selectUserByAccount(account);
                if(user!=null)
                    return DataPackage.successResponse(user);
                break;

            case GET_USERS_BY_ROOMID:
                String roomId = (String) param.get("roomId");
                List<User> list = userMapper.selectUsersByRoom(Integer.parseInt(roomId));
                if(list != null)
                    return DataPackage.successResponse(list);
                break;

            case UPDATE_USER_INFO:
                String name = (String)param.get("name");
                int headId = -1;
                if(param.get("headId")!=null)
                    headId = (int)param.get("headId");
                user = userMapper.selectUserByAccount(JwtUtils.getAccount(source.getToken()));
                if(name!=null){
                    user.setName(name);
                }
                if(headId!=-1)
                    user.setFileId(headId);
                else{
                    String fileName = (String) param.get("fileName");
                    int userId = userMapper.selectUserByAccount(JwtUtils.getAccount(source.getToken())).getUserId();
                    List<File> files =  fileMapper.selectFileByUserId(userId);
                    files.stream()
                            .forEach(e->{
                                if(e.getFileName().equals(fileName))
                                    user.setFileId(e.getFileId());
                            });
                }
                userMapper.updateUser(user);
                session.commit();


                break;
        }
        return  DataPackage.failResponse(null);
    }
}
